package com.mapaower.job.controller;

import com.mapaower.job.entity.Job;
import com.mapaower.job.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 工作职位控制器
 * 处理工作职位相关的HTTP请求
 */
@RestController
@RequestMapping("/api/jobs")
public class JobController {

    @Autowired
    private JobService jobService;

    /**
     * 创建新职位
     */
    @PostMapping
    public ResponseEntity<Job> createJob(@RequestBody Job job) {
        Job createdJob = jobService.createJob(job);
        return new ResponseEntity<>(createdJob, HttpStatus.CREATED);
    }

    /**
     * 更新职位信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Job> updateJob(@PathVariable Long id, @RequestBody Job job) {
        job.setId(id);
        Job updatedJob = jobService.updateJob(job);
        return ResponseEntity.ok(updatedJob);
    }

    /**
     * 根据ID获取职位详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Job> getJobById(@PathVariable Long id) {
        Optional<Job> job = jobService.getJobById(id);
        return job.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 获取所有开放的职位
     */
    @GetMapping("/open")
    public ResponseEntity<List<Job>> getOpenJobs() {
        List<Job> jobs = jobService.getOpenJobs();
        return ResponseEntity.ok(jobs);
    }

    /**
     * 获取所有远程工作职位
     */
    @GetMapping("/remote")
    public ResponseEntity<List<Job>> getRemoteJobs() {
        List<Job> jobs = jobService.getRemoteJobs();
        return ResponseEntity.ok(jobs);
    }

    /**
     * 根据创建者ID获取职位列表
     */
    @GetMapping("/creator/{creatorId}")
    public ResponseEntity<List<Job>> getJobsByCreatorId(@PathVariable Long creatorId) {
        List<Job> jobs = jobService.getJobsByCreatorId(creatorId);
        return ResponseEntity.ok(jobs);
    }

    /**
     * 根据类型和状态获取职位
     */
    @GetMapping("/type/{type}/status/{status}")
    public ResponseEntity<List<Job>> getJobsByTypeAndStatus(
            @PathVariable Job.JobType type,
            @PathVariable Job.JobStatus status) {
        List<Job> jobs = jobService.getJobsByTypeAndStatus(type, status);
        return ResponseEntity.ok(jobs);
    }

    /**
     * 搜索职位
     */
    @GetMapping("/search")
    public ResponseEntity<List<Job>> searchJobs(@RequestParam String keyword) {
        List<Job> jobs = jobService.searchJobs(keyword);
        return ResponseEntity.ok(jobs);
    }

    /**
     * 更新职位状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<Job> updateJobStatus(@PathVariable Long id, @RequestParam Job.JobStatus status) {
        Job updatedJob = jobService.updateJobStatus(id, status);
        return ResponseEntity.ok(updatedJob);
    }

    /**
     * 删除职位
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteJob(@PathVariable Long id) {
        jobService.deleteJob(id);
        return ResponseEntity.noContent().build();
    }
}